package mygame;

import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;
import java.util.Random;

/**
 *
 * @author Xinchro
 */
public class Pet {
    
    private String name;
    private int health;
    private int maxHealth;
    private PetType type;
    private int level;
    private int nextEXP;
    private int currentEXP;
    private int upPoints;
    private String faction;
    private int slotNo;
    private int str, dex, wis;
    private int bonusStr, bonusDex, bonusWis;
    private float[] loc;
    Main main;

    /*
     * Default constructor
     */
    public Pet(){
        name = "Pet";
        health = 10;
        type = new PetType("Fire");
        level = 1;
        nextEXP = 100;
        currentEXP = 0;
        upPoints = 0;
        faction = "Player";
	slotNo = 0;
	loc = new float[2];
	loc[0] = 0;
	loc[1] = 0;
	this.main = main;
	setupGFX();
    }
    
    public Pet(Main main, String name){
        this.name = name;
        health = 10;
        type = new PetType("Fire");
        level = 1;
        nextEXP = 100;
        currentEXP = 0;
        upPoints = 0;
        faction = "Player";
	slotNo = 0;
	loc = new float[2];
	loc[0] = 0;
	loc[1] = 0;
	str = 2;
	dex = 2;
	wis = 2;
	this.main = main;
	setupGFX();
    }
    
    BoxCollisionShape petAtkBox, petBox;
    GhostControl petGhost, petAtkGhost;
    Spatial petModel;
    Material mat;
    
    public void setupGFX(){
	petModel = main.getAssetManager().loadModel("Models/pet.j3o");
	mat = new Material(main.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
	mat.setBoolean("UseMaterialColors",true);
	mat.setColor("Diffuse", ColorRGBA.Blue);
	petBox = new BoxCollisionShape(new Vector3f(2.0f, 2.0f, 2.0f));
	petAtkBox = new BoxCollisionShape(new Vector3f(10.0f, 2.0f, 10.0f));
	petGhost = new GhostControl(petBox);
	petAtkGhost = new GhostControl(petAtkBox);
	petModel.addControl(petGhost);
	petModel.addControl(petAtkGhost);
	main.getBulletAppState().getPhysicsSpace().add(petGhost);
	main.getBulletAppState().getPhysicsSpace().add(petAtkGhost);
    }
    
    public void changeColorTo(String color){
	switch(color.toLowerCase()){
	    case "red":
		mat.setColor("Diffuse", ColorRGBA.Red);
		break;
	    case "green":
		mat.setColor("Diffuse", ColorRGBA.Green);
		break;
	    case "blue":
		mat.setColor("Diffuse", ColorRGBA.Blue);
		break;
	}
	mat.setBoolean("UseMaterialColors",true);
	petModel.setMaterial(mat);
    }
    
    public void reset(Vector3f resetLoc){
	petModel.setLocalTranslation(resetLoc);
    }
    
    public void setEnemy(){
	faction = "Enemy";
    }
    
    public void randomLoc(){
	Random rand = new Random();
	int X = rand.nextInt(100);
	int Z = rand.nextInt(100);
	int pickcorner;
	
	int pickaxis;
	
	pickaxis = rand.nextInt(2);
	
	switch(pickaxis){
	    case 0:
		while(X < 80){
		    X = rand.nextInt(100);
		}
		break;
	    case 1:
		while(Z < 80){
		    Z = rand.nextInt(100);
		}
		break;
	}
	
	pickcorner = rand.nextInt(4);
		
	switch(pickcorner){
	    case 0:
		petModel.setLocalTranslation(X, 0, Z);
		loc[0] = X;
		loc[1] = Z;
		break;
	    case 1:
		petModel.setLocalTranslation(-X, 0, Z);
		loc[0] = -X;
		loc[1] = Z;
		break;
	    case 2:
		petModel.setLocalTranslation(X, 0, -Z);
		loc[0] = X;
		loc[1] = -Z;
		break;
	    case 3:
		petModel.setLocalTranslation(-X, 0, -Z);
		loc[0] = -X;
		loc[1] = -Z;
		break;
	}
    }
    
    public void setType(PetType type){
	this.type = type;
    }
    
    public void setRandType(){
	int rand;
	Random ran = new Random();
	PetType randType = null;
	
	rand = ran.nextInt(3);
	
	switch(rand){
	    case 0:
		randType = new PetType("Fire");
		changeColorTo("red");
		break;
	    case 1:
		randType = new PetType("Water");
		changeColorTo("blue");
		break;
	    case 2:
		randType = new PetType("Nature");
		changeColorTo("green");
		break;
	}
	
	this.type = randType;
	
    }
    
    public void setLoc(float x, float y){
	loc[0] = x;
	loc[1] = y;
    }
    
    public Vector3f follow(Vector3f folLoc){
	float dx, dy, tarX, tarY, curX, curY;
	Vector3f followLoc = folLoc;
	tarX = folLoc.getX();
	tarY = folLoc.getY();
	Vector3f currentLoc = new Vector3f(this.loc[0], this.loc[1], 0.0f);
	curX = folLoc.getX();
	curY = folLoc.getY();
	Vector3f nextLoc = new Vector3f();
	
	nextLoc = followLoc.subtract(currentLoc);
	
	return nextLoc;
    }

    public float[] getLoc() {
	return loc;
    }
    
    public void attack(Pet pet){
	pet.reduceHealth(this.str, this.type);
    }
    
    public void reduceHealth(int amt, PetType atkType){
	int dmg = 0;
	
	switch(type.getElement().toLowerCase()){
	    case "fire":
		System.out.println("ele switch 1");
		switch(atkType.getElement().toLowerCase()){
		    case "fire":
			dmg = -amt;
			System.out.println("ele switch 2");
			break;
		    case "water":
			dmg = amt*2;
			break;
		    case "nature":
			dmg = 0;
			break;
		}
		break;
	    case "water":
		switch(atkType.getElement().toLowerCase()){
		    case "fire":
			dmg = 0;
			break;
		    case "water":
			dmg = -amt;
			break;
		    case "nature":
			dmg = amt*2;
			break;
		}
		break;
	    case "nature":
		switch(atkType.getElement().toLowerCase()){
		    case "fire":
			dmg = amt*2;
			break;
		    case "water":
			dmg = 0;
			break;
		    case "nature":
			dmg = -amt;
			break;
		}
		break;
	}
	System.out.println(atkType.getElement() + " is hitting " + type.getElement() + " for " + dmg);
	
	
	if(health >= 1){
	    health -= dmg;
	    System.out.println(name + " is hit!");
	}else{
	    System.out.println(name + " is dead!");
	}
    }
    
    public void raiseTemp(String stat, int amt){
	switch(stat.toLowerCase()){
	    case "str":
		bonusStr=amt;
		break;
	    case "dex":
		bonusDex+=amt;
		break;
	    case "wis":
		bonusWis+=amt;
		break;
	}
    }
    
    public void raisePerm(String stat, int amt){
	switch(stat.toLowerCase()){
	    case "str":
		str+=amt;
		break;
	    case "dex":
		dex+=amt;
		break;
	    case "wis":
		wis+=amt;
		break;
	}	
    }
    
    public void heal(int healAmt){
	if(health+healAmt<maxHealth){
	    if(DebugMode.debug){
	    System.out.print("Healing " + health + " with " + healAmt);}
	    health += healAmt;	
	    if(DebugMode.debug){
	    System.out.println(" to " + health + " out of " + maxHealth);}
	}else{
	    if(DebugMode.debug){
	    System.out.print("Healing " + health + " with " + healAmt);}
	    health = maxHealth;
	    if(DebugMode.debug){
	    System.out.println(" to " + health + " out of " + maxHealth);}
	}
    }
    
    public String getName() {
        return name;
    }

    public int getHealth() {
        return health;
    }

    public PetType getType() {
        return type;
    }

    public int getLevel() {
        return level;
    }

    public int getNextEXP() {
        return nextEXP;
    }

    public int getCurrentEXP() {
        return currentEXP;
    }

    public int getUpPoints() {
        return upPoints;
    }
    
    public String getFaction() {
        return faction;
    }
   
}
